package com.example.demo.service;



import com.example.demo.dto.AddCartDto;
import com.example.demo.dto.CartDto;
import com.example.demo.entity.Shopping;
import com.example.demo.entity.User;
import com.example.demo.mapper.ShoppingMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Dfrus
 * @Date 2022/2/17 20:07
 **/
@Service
@AllArgsConstructor
public class ShoppingCartService {
    private ShoppingMapper shoppingMapper;

    public List<Shopping> selectCart(Integer id){
        return shoppingMapper.selectCart(id);
    }

    public void updateCart(CartDto cartDto){
        shoppingMapper.updateNum(cartDto);
    }

    public Shopping findOne(CartDto cartDto){
        return shoppingMapper.selectOne(cartDto.getCartId());
    }


    public void findDelete(Integer id){
        shoppingMapper.deleteCart(id);
    }

    public User findUserId(String userName){return shoppingMapper.selectUserId(userName);}


    public List<Shopping> findAccount(Integer[] accountDtos) {
        return shoppingMapper.selectAccount(accountDtos);
    }

    public void addCart(AddCartDto addCartDto) {

        AddCartDto user = shoppingMapper.findUserList(addCartDto);

        if(null == user) {
            Date date = new Date();
            SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            addCartDto.setCartTime(fm.format(date));
            shoppingMapper.addCart(addCartDto);
        }else {
            addCartDto.setCartNum(user.getCartNum() + addCartDto.getCartNum());
            shoppingMapper.updateCartNumBySkuId(addCartDto);
        }
    }
}
